package com.fzy.core.spring;

import ge.core.system.service.DictEntryListVo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.fzy.core.entity.City;
import com.fzy.core.entity.District;
import com.fzy.core.entity.Province;
import com.fzy.core.entity.Resource;
import com.fzy.core.entity.Rssc;
import com.fzy.core.system.service.MemoryDataService;

public class M {
	private static final Logger log = Logger.getLogger(M.class);
	
	private static List provinces;
	
	private static List cities;
	
	private static List cities_2;
	
	private static List districts;
	
	private static String pcdJson;
	
	private static List areaManger;
	
	private static List dictionary;
	
	private static Map resource;
	
	private static String netAreaMangerJson;
	
	private static List rssc;
	
	private static MemoryDataService memoryDataService;	
	
	private static List roles;
	
	public static void refreshDictionary(){
		dictionary = memoryDataService.getDictionary();
	}
	
	public static void clearDictionary(){
		dictionary = null;
	}	
	
	public static void refreshProvinces(){
		provinces = memoryDataService.getProvinces();
	}
	
	public static void clearProvinces(){
		provinces = null;
	}
	
	public static void refreshCities(){
		cities = memoryDataService.getCities();
	}
	
	public static void clearCities(){
		cities = null;
	}
	
	public static void refreshCities_2(){
		cities_2 = memoryDataService.getCities_2();
	}
	
	public static void clearCities_2(){
		cities_2 = null;
	}
	
	public static void refreshDistricts(){
		districts = memoryDataService.getDistricts();
	}
	
	public static void clearDistricts(){
		districts = null;
	}
	
	public static void refreshAreaManger(){
		areaManger = memoryDataService.getAreaManger();
	}
	
	public static void clearAreaManger(){
		areaManger = null;
	}
	
	public static void refreshRssc(){
		rssc = memoryDataService.getRssc();
	}
	
	public static void clearRssc(){
		rssc = null;
	}
	
	public static void refreshResource(){
//		List list = memoryDataService.getResources();
//		resource=new TreeMap();
//		for(Iterator it = list.iterator();it.hasNext();){
//			Resource r=(Resource)it.next();
//			resource.put(r.getResourceString(), r);
//		}
	}
	
	public static void clearResource(){
		resource = null;
	}	
	
	public static void refreshNetAreaMangerJson(){
//		JSONObject namJson = new JSONObject();
//		try {
//			JSONArray net = new JSONArray();
//			List netList=getDictByType("2101");
//			for (Iterator iterator = netList.iterator(); iterator.hasNext();) {
//				DictEntryListVo s = (DictEntryListVo) iterator.next();
//				JSONArray e = new JSONArray();
//				e.put(s.getCode());
//				e.put(s.getName());
//				e.put(s.getParentCode());
//				net.put(e);
//			}
//			namJson.put("netType", net);
//			JSONArray areaMng = new JSONArray();
//			for (Iterator iterator = areaManger.iterator(); iterator.hasNext();) {
//				DictEntryListVo mng = (DictEntryListVo) iterator.next();
//				JSONArray e = new JSONArray();
//				e.put(mng.getCode());
//				e.put(mng.getName());
//				e.put(mng.getParentCode());
//				areaMng.put(e);
//			}
//			namJson.put("areaMng", areaMng);			
//		} catch (JSONException e) {
//			log.error("", e);
//		}
//		netAreaMangerJson = namJson.toString();
	}
	
	public static void clearNetAreaMangerJson(){
		netAreaMangerJson = null;
	}
	
	public static void refreshPcdJson(){
		JSONObject pcd = new JSONObject();
		try {
			JSONArray p = new JSONArray();
			for (Iterator iterator = provinces.iterator(); iterator.hasNext();) {
				Province s = (Province) iterator.next();
				JSONArray e = new JSONArray();
				e.put(s.getCode());
				e.put(s.getName());
				e.put(s.getParentCode());
				p.put(e);
			}
			pcd.put("province", p);
			JSONArray c = new JSONArray();
			for (Iterator iterator = cities.iterator(); iterator.hasNext();) {
				City city = (City) iterator.next();
				JSONArray e = new JSONArray();
				e.put(city.getCode());
				e.put(city.getName());
				e.put(city.getParentCode());
				c.put(e);
			}
			pcd.put("city", c);
			JSONArray d = new JSONArray();
			for (Iterator iterator = districts.iterator(); iterator.hasNext();) {
				District s = (District) iterator.next();
				JSONArray e = new JSONArray();
				e.put(s.getCode());
				e.put(s.getName());
				e.put(s.getParentCode());
				d.put(e);
			}
			pcd.put("district", d);
		} catch (JSONException e) {
			log.error("", e);
		}
		pcdJson = pcd.toString();
		log.info(pcdJson);
	}
	
	public static void clearPcdJson(){
		pcdJson = null;
	}	
	
	public static void refreshRoles(){
//		roles = memoryDataService.getRoles();
	}
	
	public static void clearRoles(){
		roles = null;
	}
	
	public static List getRoles() {
		return roles;
	}

	public static String getPcdJson() {
		return pcdJson;
	}
	public static List getAreaManger() {
		return areaManger;
	}
	public static List getDictionary() {
		return dictionary;
	}	
	public static List getProvinces() {
		return provinces;
	}

	public static List getCities() {
		return cities;
	}

	public static List getCities_2() {
		return cities_2;
	}

	public static List getDistricts() {
		return districts;
	}

	public static String getNetAreaMangerJson() {
		return netAreaMangerJson;
	}
	
	public static Map getResource() {
		return resource;
	}

	public void setMemoryDataService(MemoryDataService memoryDataService) {
		M.memoryDataService = memoryDataService;
	}

	public static void refreshAll(){
//		refreshProvinces();
//		refreshCities();
//		refreshDistricts();
//		refreshCities_2();
		refreshDictionary();
//		refreshPcdJson();
//		refreshAreaManger();
//		refreshNetAreaMangerJson();
//		refreshRssc();
//		refreshResource();
//		refreshRoles();
	}
	
	public void afterPropertiesSet(){
		refreshAll();
	}	
	
	public void destroy(){
		clearProvinces();
		clearCities();
		clearCities_2();
		clearDistricts();
		clearDictionary();	
		clearAreaManger();
		clearPcdJson();
		clearNetAreaMangerJson();
		clearRssc();
		clearResource();
		clearRoles();
	}
	
	
	public static Province getProvinceByCode(String code){
		Province result = null;
		if(code == null) return result;
		for (Iterator iterator = getProvinces().iterator(); iterator.hasNext();) {
			Province entity = (Province) iterator.next();
			if(code.equals(entity.getCode()))
				return entity;
		}
		return result;
	}
	
	public static City getCityByCode(String code){
		City result = null;
		if(code == null) return result;
		for (Iterator iterator = getCities_2().iterator(); iterator.hasNext();) {
			City entity = (City) iterator.next();
			if(code.equals(entity.getCode()))
				return entity;
		}
		return result;
	}
	
	public static District getDistrictByCode(String code){
		District result = null;
		if(code == null) return result;
		for (Iterator iterator = getDistricts().iterator(); iterator.hasNext();) {
			District entity = (District) iterator.next();
			if(code.equals(entity.getCode()))
				return entity;
		}
		return result;
	}
	
	public static String provinceCode2name(String code){
		Province e = getProvinceByCode(code);
		return e == null ? null : e.getName();
	}
	
	public static String cityCode2name(String code){
		City e = getCityByCode(code);
		return e == null ? null : e.getName();
	}
	
	public static String districtCode2name(String code){
		District e = getDistrictByCode(code);
		return e == null ? null : e.getName();
	}
	
	
	public static List getDictByType(String typeCode) {
		List result = new LinkedList();
		if(StringUtils.isBlank(typeCode)) return result;
		for (Iterator iterator = dictionary.iterator(); iterator.hasNext();) {
			DictEntryListVo vo = (DictEntryListVo) iterator.next();
			if(typeCode.equals(vo.getTypeCode()))
				result.add(vo);
		}
		return result;
	}
	
	public static String getDictJsonByType(String typeCode) {
		List list = getDictByType(typeCode);
		JSONArray a = new JSONArray();
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			DictEntryListVo s = (DictEntryListVo) iterator.next();
			JSONArray e = new JSONArray();
			e.put(s.getCode());
			e.put(s.getName());
			e.put(s.getParentCode());
			a.put(e);
		}
		return a.toString();
	}
	
	
	public static List getDictByTypeForSelect(String typeCode) {
		List l = getDictByType(typeCode);
		DictEntryListVo dictEntryListVo = new DictEntryListVo();
		dictEntryListVo.setCode("");
		dictEntryListVo.setName("请选择");
		l.add(0, dictEntryListVo);
		return l;
	}
	
	/**根据字典类型和编码取得字典名称
	 * @param code 编码
	 * @param typeCode 字典类型
	 * @return
	 */
	public static String getDictNameByTypeAndCode(String code, String typeCode) {
		String result = "";
		DictEntryListVo vo = getDictByTypeAndCode(code, typeCode);
		if(vo != null) result = StringUtils.defaultString(vo.getName());
		return result;
	}
	
	public static DictEntryListVo getDictByTypeAndCode(String code, String typeCode) {
		DictEntryListVo result = null;
		if(StringUtils.isBlank(typeCode) || StringUtils.isBlank(code)) return result;
		for (Iterator iterator = dictionary.iterator(); iterator.hasNext();) {
			DictEntryListVo vo = (DictEntryListVo) iterator.next();
			if(typeCode.equals(vo.getTypeCode()) && code.equals(vo.getCode()))
				return vo;
		}
		return result;
	}
	
	public static DictEntryListVo getUserInfo(String userId){
//		return (DictEntryListVo)M.memoryDataService.getUserEmployeeInfo(userId);	
		return null;
	}
	
	public static String areaManagerUserId2name(String id){
		String result = null;
		if(id == null) return result;
		DictEntryListVo entity = (DictEntryListVo)getUserInfo(id);
		if(entity==null) return result;
		return entity.getName();
	}
	
	public static Rssc getRsscByCode(String code){
		Rssc result = null;
		if(code == null) return result;
		for (Iterator iterator = rssc.iterator(); iterator.hasNext();) {
			Rssc entity = (Rssc) iterator.next();
			if(code.equals(entity.getCode()))
				return entity;
		}
		return result;
	}
	public static List getRssc() {
		return rssc;
	}
	public static List getRsscForSelect(){
		List result = new ArrayList();
		result.addAll(getRssc());
		Rssc r = new Rssc();
		r.setCode("");
		r.setName("请选择");
		result.add(0,r);
		return result;
	}
	public static String rsscCode2name(String code){
		Rssc e = getRsscByCode(code);
		return e == null ? null : e.getName();
	}
}
